package im.yisme.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import android.util.Log;

import im.yisme.model.fileio.LyricsRepositoryReader;
import im.yisme.model.fileio.LyricsRepositoryWriter;
import im.yisme.model.service.LyricsRepositoryService;

import im.yisme.vo.LyricsVO;
import im.yisme.vo.MusicVO;

public class LyricsRepository implements LyricsRepositoryService{

	private static LyricsRepository lyricsRepository;
	private ArrayList<Lyrics> list;
	
	private LyricsRepository(){
		
	}
	
	public static LyricsRepository getInstance(){
		return lyricsRepository == null ? lyricsRepository = new LyricsRepository() : lyricsRepository;
	}
	
	public Iterator<LyricsVO> getLyricsList(){
		if (list == null)
			return null;
		
		ArrayList<LyricsVO> tempList = new ArrayList<LyricsVO>();
		for (Lyrics lyrics: list){
			tempList.add(lyrics.toVO());
		}
		return tempList.iterator();
	}
	
	public ArrayList<Lyrics> getRealList(){
		return list;
	}
	
	public void setLyricsList(ArrayList<Lyrics> list){
		this.list = list;
	}
	
	public void loadFile(){
		LyricsRepositoryReader reader = new LyricsRepositoryReader();
		try {
			reader.read(this);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Iterator<LyricsVO> getLyricsForMusic(MusicVO music) {
		// TODO Auto-generated method stub
		ArrayList<LyricsVO> result = new ArrayList<LyricsVO>();
		for (Lyrics lyrics : list){
			String name = music.getName();
			String artist = music.getArtist();
			if (name.equals(lyrics.getName()) && artist.equals(lyrics.getArtist()))
				result.add(lyrics.toVO());
		}
		return result.iterator();
	}
	
	public LyricsVO findLyrics(long lyricsId){
		LyricsVO result = null;
		
		for (Lyrics lyrics : list){
			if (lyrics.getId() == lyricsId){
				result = lyrics.toVO();
				break;
			}
		}
		
		return result;
	}
	
	public Lyrics getLyrics(long lyricsId){
		for (Lyrics lyrics : list){
			if (lyrics.getId() == lyricsId){
				return lyrics;
			}
		}
		return null;
	}

	public long addLyricsToRepository(LyricsVO toAdd) {
		// TODO Auto-generated method stub
		try {
			Lyrics lyrics = new Lyrics(getNewId(), toAdd.getArtist(), toAdd.getName(), toAdd.getFilePath(), toAdd.getSource());
			lyrics.setNoteNum(toAdd.getNoteNum());	
			lyrics.setTotalTime(toAdd.getTotalTime());
			list.add(lyrics);
			updateLocalData();
			return lyrics.getId();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		}
	}

	public boolean removeLyricsFromRepository(LyricsVO toRemove) {
		// TODO Auto-generated method stub
		try {
			for (Lyrics lyrics : list){
				if (lyrics.getId() == toRemove.getId()){
					list.remove(lyrics);
					break;
				}
			}
			updateLocalData();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean alterLyricsInfo(LyricsVO lyricsVO) {
		// TODO Auto-generated method stub
		try {
			for (Lyrics lyrics : list){
				if (lyrics.getId() == lyricsVO.getId()){
					lyrics.setArtist(lyricsVO.getArtist());
					lyrics.setName(lyricsVO.getName());
					break;
				}
			}
			updateLocalData();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	private void updateLocalData() throws IOException{
		new LyricsRepositoryWriter().write(this);
	}
	
	private long getNewId(){
		return list.size() == 0 ? 1 : list.get(list.size()-1).getId() + 1;
	}
}
